<template>
  <div id="home">
    <van-row v-for="item in requestList" :key="item.friendRequestId" type="flex" justify="center"
             style="margin-top: 10%; ">
      <van-col span="6">
        <van-cell center>
          <van-image width="50" height="50" src="https://img.yzcdn.cn/vant/cat.jpeg"/>
        </van-cell>
      </van-col>

      <van-col span="6">
        <van-cell center :title="item.nickName" :label="item.phone"/>
      </van-col>
      <van-col span="12">
        <van-cell center>
          <van-button @click="refuseRequest(item.friendRequestId)" type="warning">忽略</van-button>
          <van-button @click="agreeRequest(item.friendRequestId)" style="margin-left: 7px" type="primary">同意
          </van-button>
        </van-cell>
      </van-col>

    </van-row>



    <div v-for="(item, index) in myFriendList" :key="index">

      <van-row >
        <van-col span="3"><van-image style="margin-top: 30%;float: right;"  width="40" height="40" src="https://img.yzcdn.cn/vant/cat.jpeg"/></van-col>
        <van-col span="21"><van-cell center :title="item.noteName" :label="item.messageContent" :to="{name:'chat', params:{acceptUserId:item.acceptUserId}}"/></van-col>
      </van-row>
    </div>



    <MainTabBar v-bind:activeValue="active"/>
  </div>
</template>

<script>

  import MainTabBar from "components/common/MainTabBar";
  import {getRequest} from "network/getRequest";

  import {Dialog} from 'vant';

  export default {
    name: "home",
    components: {
      MainTabBar

    },
    data() {
      return {
        title: '',
        label: '',
        requestList: [],
        myFriendList:[],
        active: 0,
      }
    },
    created() {
      this.initHomeData()
    },
    methods: {
      initHomeData() {
        let phone = JSON.parse(sessionStorage.getItem("userInfo")).phone;
        let params = {phone}
        getRequest('/friend/request/search', params).then(res => {
          this.requestList = res.data
        })

        getRequest('/chatMessage/phone', params).then(res => {
          this.myFriendList = res.data
        })

      },
      agreeRequest(requestId) {
        let params = {requestId}
        getRequest("/friend/request/agree", params).then(res => {
          if (res.code == 200) {
            location.reload()
          } else {
            Dialog.alert({message: res.msg})
          }
        })
      },
      refuseRequest(requestId) {
        let params = {requestId}
        getRequest("/friend/request/refuse", params).then(res => {
          if (res.code == 200) {
            location.reload()
          } else {
            Dialog.alert({message: res.msg})
          }
        })
      }

    }
  }
</script>

<style>
  .goods-card {
    margin: 0;
    height: 50px;
    background-color: @white;
  }

  .delete-button {
    height: 100%;
  }
</style>